home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_02_04 / 2n04075a < prev    next >
Text File  |  1991-02-16  |  4KB  |  177 lines

  1. /*                 LISTING 1 (inc_sfht.c)
  2.     Make additional entries available in the System File
  3.     Handle Table.  Use this in lieu of forcing the user to
  4.     add a FILES= statement in the config.sys and re-boot.
  5.     To build an executable:
  6.         cl -AL inc_sfht.c    (MSC 5.x  NOTE: Large model.)
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <malloc.h>
  11. #include <dos.h>
  12.  
  13. // NOTE: must pack structures on byte alignment
  14. #pragma pack(1)
  15.  
  16. // --- see the article for description of these elements
  17. typedef struct list_of_lists
  18.     {
  19.     void far *dcb_head, far *handle_sft, far *clock;
  20.     void far *console;
  21.     unsigned max_sector;
  22.     void far *cache, far *cur_dir, far *fcb_sft;
  23.     unsigned unknown;
  24.     unsigned char drive_cnt, lastdrive;
  25.     } LOL, far *LOL_PTR;
  26.  
  27. // --- see the article for description of these elements
  28. typedef struct system_file_table_header
  29.     {
  30.     void far *next;
  31.     unsigned count;
  32.     } SFTH, far *SFTH_PTR;
  33.  
  34. #define SFTH_SIZE sizeof(SFTH)
  35.  
  36. // prototypes
  37. short open_files(void), add_system_handles(short);
  38. short find_size(void *);
  39. void remove_added_handles(void), close_files(void);
  40.  
  41. // place to hold the original end-of-list header
  42. static SFTH_PTR old_end_of_list = (SFTH_PTR)0;
  43.  
  44. // for the testing file structures
  45. FILE *fid_array[20];
  46.  
  47. main()
  48. {
  49.   printf("opened %d before expansion\n", open_files());
  50.   close_files();
  51.   add_system_handles(5);
  52.   printf("opened %d after expansion\n", open_files());
  53.   close_files();
  54.   remove_added_handles();
  55.   printf("opened %d after reset\n", open_files());
  56.   close_files();
  57. }
  58.  
  59. short open_files()
  60. {
  61. short i;
  62.  
  63.   for(i = 0; i < 20; i++)
  64.      if( (fid_array[i] = fopen("nul", "wt")) == NULL )
  65.         break;
  66.   return(i);
  67. }
  68.  
  69. void close_files()
  70. {
  71. short i = 0;
  72.  
  73.   while( fid_array[i] != (FILE *)NULL )
  74.      fclose(fid_array[i++]);
  75. }
  76.  
  77. short add_system_handles( short new_entries )
  78. {
  79. union REGS inregs, outregs;
  80. struct SREGS sregs;
  81. LOL_PTR lol;
  82. SFTH_PTR sfth, new_block;
  83. char *sft;
  84. short infoblk_size;
  85.  
  86.   if( old_end_of_list != (char *)NULL )
  87.      return(-1);
  88.  
  89. // get the list of list address
  90.   inregs.h.ah = 0x52;
  91.   int86x(0x21, &inregs, &outregs, &sregs);
  92.   FP_SEG(lol) = sregs.es;
  93.   FP_OFF(lol) = outregs.x.bx;
  94.  
  95. // get the 1st table header address
  96.   sfth = lol->handle_sft;
  97.  
  98. // get the actual table address
  99.   sft = (char *)sfth + SFTH_SIZE;
  100.  
  101. // find the size of a single file information block
  102.   infoblk_size = find_size(sft);
  103.   if( infoblk_size == 0 )
  104.      return(-2);
  105.  
  106. // find the last node in the SFHT
  107.   while( FP_OFF(sfth->next) != 0xffff )
  108.      sfth = sfth->next;
  109.  
  110. // save the old end address & chain on some more entries
  111.   new_block = (SFTH_PTR)calloc(new_entries,
  112.                                infoblk_size + SFTH_SIZE);
  113.   if( new_block == (SFTH_PTR)0 )
  114.      return(0);
  115.   old_end_of_list = sfth;
  116.  
  117. // init the new entry header
  118.   FP_OFF(new_block->next) = 0xffff;
  119.   new_block->count = new_entries;
  120.  
  121. // chain it in and return success
  122.   sfth->next = new_block;
  123.   return(1);
  124. }
  125.  
  126. void remove_added_handles()
  127. {
  128. // if haven't allocated, don't free
  129.   if( old_end_of_list == (SFTH_PTR)0 )
  130.      return;
  131.  
  132. // free the allocated memory, make the header indicate the
  133. // end of the list, & clear the static pointer
  134.   free(old_end_of_list->next);
  135.   FP_OFF(old_end_of_list->next) = 0xffff;
  136.   old_end_of_list = (SFTH_PTR)0;
  137. }
  138.  
  139. short find_size( char *ft_ptr )
  140. {
  141. char *ch_ptr, *aux_ptr;
  142. short i;
  143.  
  144. // search for the string "AUX" (1st info blk entry)
  145.   ch_ptr = ft_ptr;
  146.   for(i = 0; i < 100; i++ )
  147.      {
  148.      if( strncmp(ch_ptr, "AUX", 3) == 0 )
  149.         {
  150.         aux_ptr = ch_ptr;
  151.         break;
  152.         }
  153.      else
  154.         ch_ptr++;
  155.      }
  156.  
  157. // if not found return error
  158.   if( i == 100 )
  159.      return(0);
  160.  
  161. // now search for "CON" (2nd info blk entry)
  162.   for(i = 0; i < 100; i++ )
  163.      {
  164.      if( strncmp(ch_ptr, "CON", 3) == 0 )
  165.         break;
  166.      else
  167.         ch_ptr++;
  168.      }
  169.  
  170. // if not found return error
  171.   if( i == 100 )
  172.      return(0);
  173.  
  174. // return the difference (i.e. size of an info blk)
  175.   return(ch_ptr - aux_ptr);
  176. }
  177.